package com.easyj.admin.sys.serviceimpl;

import java.util.List;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.easyj.admin.sys.service.GroupService;
import com.easyj.admin.db.mapper.GroupMapper;
import com.easyj.admin.db.mapper.GroupUserMapper;
import com.easyj.base.common.page.Page;
import com.easyj.base.exception.*;
import com.easyj.base.security.service.SecurityUtil;
import com.easyj.admin.db.model.Group;
import com.easyj.admin.db.model.GroupUser;
import com.easyj.admin.db.model.User;


@Service
public class GroupServiceImpl implements GroupService{
	
	@Autowired
	private  GroupMapper groupMapper;
	
	@Autowired
	private  GroupUserMapper groupUserMapper;
	

	public Page<Group> findPage(Group filter){
	    Long total=groupMapper.countByExample(filter);
		List<Group> rows=null;
		if(total>0l){
			filter.getPage().resetOffsetByTotal(total);
		    rows= groupMapper.selectByExample(filter);
		    for(Group g:rows) {
		    	long n=groupUserMapper.countByGropuId(g.getId());
		    	String str=n+" 个用户";		    	
		    	g.setUserStr(str);
		    }
		}
		
		Page<Group> rtn=new Page<Group>(rows,total,filter.getPage());
		return rtn;
	}
	
   	public List<Group> findList(Group filter){
        return groupMapper.selectByExample(filter);
    }
	public long count(Group filter){
	     return groupMapper.countByExample(filter);
	}	

	public Group findById(Long id){
		Group entity= groupMapper.selectByPrimaryKey(id);
		return entity;
	}
	

	@Transactional
	public int  insertEntity(Group entity) throws EasyjException{
		//唯一....
		if(groupMapper.selectUniqueByUqCode(entity.getCode())!=null) {
			throw new ValidateException(EasyjException.SameCode,"代码："+entity.getCode()+"已存在");
		}
		//唯一....
		if(groupMapper.selectUniqueByUqName(entity.getName())!=null) {
			throw new ValidateException(EasyjException.SameCode,"名称："+entity.getCode()+"已存在");
		}
		entity.setIsSystem(false);
	    entity.setCreateTime(new Date());
		entity.setOperatorId(SecurityUtil.getOperatorId());	
		int rtn= groupMapper.insert(entity);
		return rtn;
	}
	
	@Transactional
	public int  updateEntity(Group entity) throws EasyjException{
		//唯一.....
		Group f1=groupMapper.selectUniqueByUqCode(entity.getCode());
  		if(f1!=null && f1.getId().longValue()!=entity.getId().longValue()) {
  			throw new ValidateException(EasyjException.SameCode,"代码："+entity.getCode()+"已存在");
  		}
		//唯一.....
		Group f=groupMapper.selectUniqueByUqName(entity.getName());
  		if(f!=null && f.getId().longValue()!=entity.getId().longValue()) {
  			throw new ValidateException(EasyjException.SameCode,"名称："+entity.getCode()+"已存在");
  		}
	    entity.setModifyTime(new Date()); 
		entity.setOperatorId(SecurityUtil.getOperatorId());	
		int rtn= groupMapper.updateByPrimaryKey(entity);
		return rtn;
	}

	@Transactional
	public int  deleteById(Long id) throws EasyjException{
		Group entity=groupMapper.selectByPrimaryKey(id);
		if(entity.getIsSystem()) { //系统角色，不能删除
	    	throw new UpdateSystemObjectException();
	    }	
		//同时删除
		groupUserMapper.deleteByGropuId(id);
		int rtn= groupMapper.deleteByPrimaryKey(id);
		
		return rtn;
	}


	public List<User> findUsersByGroupId(Long groupId){
		return groupUserMapper.selectUsersByGroupId(groupId);
	}
	
	public List<User> findUsersByGroupCode(String groupCode){
		return groupUserMapper.selectUsersByGroupCode(groupCode);
	}
	
	@Transactional
	public int addGroupUsers(List<GroupUser> records){
		int rtn=0;
		for(GroupUser r:records) {
			rtn+=groupUserMapper.insert(r);
		}
		return rtn;
	}
	
	@Transactional
	public int deleteGroupUser(GroupUser record) {
		GroupUser gu=groupUserMapper.selectUniqueByUnqueInx(record.getUserId(), record.getGroupId());
		return groupUserMapper.deleteByPrimaryKey(gu.getId());
	}
	
}
